import { config } from '../config/index'

// 获取绑定数据
export function fetchPlayerList(openId: string, turnpartition: boolean) {
  return new Promise((resolve) => {
    try {
      wx.request({
        url: config.urlBase + 'play/getPlayerById',
        method: 'GET',
        header: {
          'Content-Type': config.ContentTypeGET,
          'AuthorizationG': config.AuthorizationG
        },
        data: {
          openId: openId,
          turnpartition: turnpartition
        },
        success: (res: any) => {
          if (res.statusCode === 404) {
            wx.redirectTo({ url: config.urlEX });
            return
          }
          if (res.data.code === 0) {
            resolve(res.data.list);
          } else {
            resolve(null);
          }
        }
      });
    } catch (error) {
      console.log(error)
      wx.redirectTo({ url: config.urlEX });
    };
  });
}
// 新增绑定数据
export function bindPlayer(player: object) {
  return new Promise((resolve) => {
    try {
      wx.request({
        url: config.urlBase + 'play/insPlayer',
        method: 'POST',
        header: {
          'Content-Type': config.ContentTypePOST,
          'AuthorizationG': config.AuthorizationG
        },
        data: {
          playerName: player.userId,
          partitionId: player.partitionId,
          gamePartition: player.gameName,
          roleName: player.roleName,
          wechatName: player.wechatName,
          openId: player.openId
        },
        success: (res: any) => {
          if (res.statusCode === 404) {
            wx.setStorageSync('wechatName', null)
            wx.setStorageSync('openId', null)
            wx.redirectTo({ url: config.urlEX });
            return
          }
          if (res.data.code === 1) {
            wx.setStorageSync('wechatName', null)
            wx.setStorageSync('openId', null)
          }
          resolve(res.data.message);
        }
      });
    } catch (error) {
      resolve(res.data.message);
      wx.setStorageSync('wechatName', null)
      wx.setStorageSync('openId', null)
      wx.redirectTo({ url: config.urlEX });
    };
  });
}
// 编辑绑定数据
export function upPlayer(PlayerInfo: object) {
  return new Promise((resolve) => {
    try {
      const data = {}; // 初始化data对象
      // 动态构建data对象，只包含非空属性
      if (PlayerInfo.playerId) data.playerId = PlayerInfo.playerId;
      data.showCode = PlayerInfo.showCode;
      wx.request({
        url: config.urlBase + 'play/upPlayer',
        method: 'POST',
        header: {
          'Content-Type': config.ContentTypePOST,
          'AuthorizationG': config.AuthorizationG
        },
        data: data,
        success: (res: any) => {
          if (res.statusCode === 404) {
            wx.redirectTo({ url: config.urlEX });
            return
          }
          if (res.data.code === 0) {
            resolve(0);
          } else {
            resolve(1);
          }
        }
      });
    } catch (error) {
      resolve(1);
      wx.redirectTo({ url: config.urlEX });
    };
  });
}
//获取转区金额
export function getPlayerPrice(formPlayer: object) {
  return new Promise((resolve) => {
    try {
      wx.request({
        url: config.urlBase + 'topUp/getPlayerPrice',
        method: 'POST',
        header: {
          'Content-Type': config.ContentTypePOST,
          'AuthorizationG': config.AuthorizationG
        },
        data: {
          topupUser: formPlayer.userName,
          topupPartition: formPlayer.gameName
        },
        success: (res: any) => {
          if (res.statusCode === 404) {
            wx.redirectTo({ url: config.urlEX });
            return
          }
          resolve(res.data)
        }
      });
    } catch (error) {
      resolve('');
      wx.redirectTo({ url: config.urlEX });
    };
  });
}
//检测转区码是否重复
export function checkTurnCode(turnCode: string) {
  return new Promise((resolve) => {
    try {
      wx.request({
        url: config.urlBase + 'turn/checkTurnCode',
        method: 'POST',
        header: {
          'Content-Type': config.ContentTypePOST,
          'AuthorizationG': config.AuthorizationG
        },
        data: {
          turnCode: turnCode
        },
        success: (res: any) => {
          if (res.statusCode === 404) {
            wx.redirectTo({ url: config.urlEX });
            return
          }
          if (res.data.code === 0) {
            resolve(true);
          } else {
            resolve(false);
          }
        }
      });
    } catch (error) {
      resolve(false);
      wx.redirectTo({ url: config.urlEX });
    };
  });
}
// 新增绑定数据
export function insTurn(turn: object, turnCode: string) {
  return new Promise((resolve) => {
    try {
      wx.request({
        url: config.urlBase + 'turn/insTurn',
        method: 'POST',
        header: {
          'Content-Type': config.ContentTypePOST,
          'AuthorizationG': config.AuthorizationG
        },
        data: {
          turnOld: turn.gameName,
          turnNew: turn.turnPartition,
          gameUser: turn.userName,
          turnCode: turnCode,
          turnPrice: turn.turnPrice
        },
        success: (res: any) => {
          if (res.statusCode === 404) {
            wx.redirectTo({ url: config.urlEX });
            return
          }
          resolve(res.data);
        }
      });
    } catch (error) {
      resolve(res.data.message);
      wx.redirectTo({ url: config.urlEX });
    };
  });
}
// 获取open_id
export function checkLogin() {
  return new Promise((resolve) => {
    try {
      wx.login({
        success: res => {
          if (res.code) {
            // 将 code 发送到后端服务器进行登录验证
            wx.request({
              url: config.urlBase + 'chatLogin',
              method: 'POST',
              header: {
                'Content-Type': config.ContentTypePOST,
                'AuthorizationG': config.AuthorizationG
              },
              data: {
                code: res.code,
              },
              success: (res) => {
                resolve(res.data.message);
              },
              fail: function (error) {
                // 处理请求失败的情况
                console.log('登录失败：');
                console.log(error);
              }
            });
          } else {
            console.log('登录获取code失败！' + res.errMsg);
          }
        }
      });
    } catch (error) {
      resolve('');
      wx.redirectTo({ url: config.urlEX });
    }
  })
}